package com.bw.merchant.view

import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.constants.RoutePath
import com.bw.common.view.BaseMvvmActivity
import com.bw.merchant.R
import com.bw.merchant.adapter.MerchantAdapter
import com.bw.merchant.databinding.ActivitySettledBinding
import com.bw.merchant.entity.MerchantEntity
import com.bw.merchant.net.SettledViewModel

/**
 * 智慧园区-04-商家推荐V1.1
 * 入驻审批
 * 兰宏宇
 * 20250509
 */
@Route(path = RoutePath.PATH_MERCHANT_SETTLED)
class SettledActivity : BaseMvvmActivity<ActivitySettledBinding, SettledViewModel>() {
    private lateinit var merchantAdapter: MerchantAdapter
    private val mList: MutableList<MerchantEntity> = mutableListOf()
    private var merchantState:Int = 0

    override fun initView() {
        merchantAdapter = MerchantAdapter()
        binding.rvSettled.adapter = merchantAdapter
        binding.rvSettled.layoutManager = LinearLayoutManager(this)
        binding.ivBack.setOnClickListener { finish() }

        merchantAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build(RoutePath.PATH_MERCHANT_SETTLED_DETAIL)
                .withSerializable("merchant",merchantAdapter.data[position])
                .navigation()
        }


        binding.tvD.setOnClickListener {
            merchantStateChange(0)
        }

        binding.tvY.setOnClickListener {
            merchantStateChange(1)
        }

        binding.tvApply.setOnClickListener {
            ARouter.getInstance().build(RoutePath.PATH_MERCHANT_SETTLED_APPLY).navigation()
        }


    }

    override fun initData() {
        vm.merchantSuccess.observe(this) {
            mList.clear()
            mList.addAll(it)
            merchantStateChange(merchantState)
        }


    }

    override fun onResume() {
        super.onResume()
        vm.getMerchant()
    }


    fun merchantStateChange(state:Int){
        merchantState = state
        if(state==0){
            binding.tvD.setBackgroundResource(R.drawable.bg_select_green)
            binding.tvD.setTextColor(Color.parseColor("#ffffff"))
            binding.tvY.setBackgroundResource(R.drawable.bg_unselect_white)
            binding.tvY.setTextColor(Color.parseColor("#6B7280"))

            merchantAdapter.data.clear()
            merchantAdapter.data.addAll(mList.filter { merchantEntity -> merchantEntity.status != "已审核" })
            merchantAdapter.notifyDataSetChanged()
        }else{
            binding.tvY.setBackgroundResource(R.drawable.bg_select_green)
            binding.tvY.setTextColor(Color.parseColor("#ffffff"))
            binding.tvD.setBackgroundResource(R.drawable.bg_unselect_white)
            binding.tvD.setTextColor(Color.parseColor("#6B7280"))

            merchantAdapter.data.clear()
            merchantAdapter.data.addAll(mList.filter { merchantEntity -> merchantEntity.status == "已审核" })
            merchantAdapter.notifyDataSetChanged()
        }
    }

}